package com.pengheng.util;

import cn.hutool.json.JSONUtil;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class DateRange {

    public static void main(String[] args) {
        String startDate = "2024-04-01";
        String endDate = "2024-04-18";

        List<String> dateRange = getDateRange(startDate, endDate);

        System.out.println(JSONUtil.toJsonStr(dateRange));
    }

    public static List<String> getDateRange(String startDate, String endDate) {
        List<String> list = new ArrayList<>();
        // 将字符串转换为 LocalDate 类型
        LocalDate start = LocalDate.parse(startDate, DateTimeFormatter.ISO_DATE);
        LocalDate end = LocalDate.parse(endDate, DateTimeFormatter.ISO_DATE);

        // 创建一个日期范围
        LocalDate currentDate = start;
        while (currentDate.compareTo(end) <= 0) {
            // 将 LocalDate 格式的日期转换为 yyyy-MM-dd 字符串格式
            String dateString = currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            list.add(dateString);
            currentDate = currentDate.plusDays(1);
        }
        return list;
    }
}